#include 	"main.h"
#include 	"system.h"
#include	"stm32f0xx_hal_gpio_ex.h"
#include 	"stm32f0xx_hal_uart.h"
void SendDataToDisplayBoard(uint8 *p, uint8 length)
{
   
    HAL_UART_Transmit(&CommunicationUart,p,length, 0xFFFF);
}

uint8 SumCheck(uint8 *p, uint16 length)
{
    uint16 sum = 0;
    uint8 *point = p;
    uint16 i = 0;
    uint16 crc = 0;
    
    for (i = 0; i < length - 2; i++)
    {
        sum += (*(point + i));
    }
    crc = (((uint16) * (p + length - 1)) << 8) | ((uint16) * (p + length - 2));
    if (sum == crc)
    {
        return 1;
    }
    
    else
    {
        return 0;
    }
}
uint16 SumCalculate(uint8 *p, uint8 length)
{
    uint16 sum = 0;
    uint8 *point = p;
    uint8 i = 0;
    
    for (i = 0; i < length - 2; i++)
    {
        sum += (*(point + i));
    }
    return sum;
}

void SendCollectedValToDisplayBoard(CollectedDataType datatype,  CollectedData *result)
{
    uint8 i = 0;
	uint16  crc=0;
    uint8 sendbuffer[35] ={0x55,0XAA,0X01,  0,0,0,0,0,0,0,0,  0,0,0,0,0,0,0,0,0,0,0};

    if(datatype==CurrentValue)
    {
		
		sendbuffer[2]=CurrentValue;

		sendbuffer[3]=(uint8)(result->CurData.aVal>>8);
		sendbuffer[4]=(uint8)(result->CurData.aVal&0X00FF);
		
		sendbuffer[5]=(uint8)(result->CurData.bVal>>8);
		sendbuffer[6]=(uint8)(result->CurData.bVal&0X00FF);
		
		sendbuffer[7]=(uint8)(result->CurData.cVal>>8);
		sendbuffer[8]=(uint8)(result->CurData.cVal&0X00FF);
		
		sendbuffer[9]=(uint8)(result->CurData.rVal>>8);
		sendbuffer[10]=(uint8)(result->CurData.rVal&0X00FF);

	}
	else if(datatype==TemperatureValue)
	{
		
		sendbuffer[2]=TemperatureValue;

		
		sendbuffer[3]=(uint8)(result->TemData.aVal>>8);
		sendbuffer[4]=(uint8)(result->TemData.aVal&0X00FF);
		
		sendbuffer[5]=(uint8)(result->TemData.bVal>>8);
		sendbuffer[6]=(uint8)(result->TemData.bVal&0X00FF);
		
		sendbuffer[7]=(uint8)(result->TemData.cVal>>8);
		sendbuffer[8]=(uint8)(result->TemData.cVal&0X00FF);

		sendbuffer[9]=(uint8)(result->TemData.sVal>>8);
		sendbuffer[10]=(uint8)(result->TemData.sVal&0X00FF);
	}
	else if(datatype==VoltageValue)
	{
		
		sendbuffer[2]=VoltageValue;

		
		sendbuffer[3]=(uint8)(result->VolData.aVal>>8);
		sendbuffer[4]=(uint8)(result->VolData.aVal&0X00FF);
		
		sendbuffer[5]=(uint8)(result->VolData.bVal>>8);
		sendbuffer[6]=(uint8)(result->VolData.bVal&0X00FF);
		
		sendbuffer[7]=(uint8)(result->VolData.cVal>>8);
		sendbuffer[8]=(uint8)(result->VolData.cVal&0X00FF);
		
	}
	else if(datatype==IMEIValue)
	{
		
		sendbuffer[2]=IMEIValue;
		memcpy(sendbuffer+3,result->BC26_IMEI,IMEI_Len);

	}
	else if(datatype==DEVEUI)
	{
		
		sendbuffer[2]=DEVEUI;
		memcpy(sendbuffer+3,result->LORA_DEVEUI,DEVEUI_Len);

	}	
	else if(datatype==ThresholdValue)
	{
		printf("ThresholdVal\n");
		sendbuffer[2]=ThresholdValue;
		
		sendbuffer[3]=(uint8)(SysPara.thresholdVal.CurThreshold_a>>8);
		sendbuffer[4]=(uint8)(SysPara.thresholdVal.CurThreshold_a&0X00FF);

		sendbuffer[5]=(uint8)(SysPara.thresholdVal.CurThreshold_b>>8);
		sendbuffer[6]=(uint8)(SysPara.thresholdVal.CurThreshold_b&0X00FF);

		sendbuffer[7]=(uint8)(SysPara.thresholdVal.CurThreshold_c>>8);
		sendbuffer[8]=(uint8)(SysPara.thresholdVal.CurThreshold_c&0X00FF);

		sendbuffer[9]=(uint8)(SysPara.thresholdVal.CurThreshold_r>>8);
		sendbuffer[10]=(uint8)(SysPara.thresholdVal.CurThreshold_r&0X00FF);
		

		sendbuffer[11]=(uint8)(SysPara.thresholdVal.Vol_OverThreshold_a>>8);
		sendbuffer[12]=(uint8)(SysPara.thresholdVal.Vol_OverThreshold_a&0X00FF);
		
		sendbuffer[13]=(uint8)(SysPara.thresholdVal.Vol_OverThreshold_b>>8);
		sendbuffer[14]=(uint8)(SysPara.thresholdVal.Vol_OverThreshold_b&0X00FF);
		
		sendbuffer[15]=(uint8)(SysPara.thresholdVal.Vol_OverThreshold_c>>8);
		sendbuffer[16]=(uint8)(SysPara.thresholdVal.Vol_OverThreshold_c&0X00FF);

		sendbuffer[17]=(uint8)(SysPara.thresholdVal.Vol_BelowThreshold_a>>8);
		sendbuffer[18]=(uint8)(SysPara.thresholdVal.Vol_BelowThreshold_a&0X00FF);
		
		sendbuffer[19]=(uint8)(SysPara.thresholdVal.Vol_BelowThreshold_b>>8);
		sendbuffer[20]=(uint8)(SysPara.thresholdVal.Vol_BelowThreshold_b&0X00FF);
		
		sendbuffer[21]=(uint8)(SysPara.thresholdVal.Vol_BelowThreshold_c>>8);
		sendbuffer[22]=(uint8)(SysPara.thresholdVal.Vol_BelowThreshold_c&0X00FF);




		sendbuffer[23]=(uint8)(SysPara.thresholdVal.TemThreshold_a>>8);
		sendbuffer[24]=(uint8)(SysPara.thresholdVal.TemThreshold_a&0X00FF);
		
		sendbuffer[25]=(uint8)(SysPara.thresholdVal.TemThreshold_b>>8);
		sendbuffer[26]=(uint8)(SysPara.thresholdVal.TemThreshold_b&0X00FF);
		
		sendbuffer[27]=(uint8)(SysPara.thresholdVal.TemThreshold_c>>8);
		sendbuffer[28]=(uint8)(SysPara.thresholdVal.TemThreshold_c&0X00FF);
		
		sendbuffer[29]=(uint8)(SysPara.thresholdVal.TemThreshold_s>>8);
		sendbuffer[30]=(uint8)(SysPara.thresholdVal.TemThreshold_s&0X00FF);

		
	}
	else if(datatype==ChannleEnableValue)
	{
		printf("ChannleEnableValue\n");
		sendbuffer[2]=ChannleEnableValue;
		
		sendbuffer[3]=SysPara.ChannleStatus.CurChannle_a;
		sendbuffer[4]=SysPara.ChannleStatus.CurChannle_b;
		sendbuffer[5]=SysPara.ChannleStatus.CurChannle_c;
		sendbuffer[6]=SysPara.ChannleStatus.CurChannle_r;

		sendbuffer[7]=SysPara.ChannleStatus.VolChannle_a;
		sendbuffer[8]=SysPara.ChannleStatus.VolChannle_b;
		sendbuffer[9]=SysPara.ChannleStatus.VolChannle_c;

		sendbuffer[10]=SysPara.ChannleStatus.TemChannle_a;
		sendbuffer[11]=SysPara.ChannleStatus.TemChannle_b;
		sendbuffer[12]=SysPara.ChannleStatus.TemChannle_c;
		sendbuffer[13]=SysPara.ChannleStatus.TemChannle_s;

		
	}

	if(datatype==IMEIValue||datatype==DEVEUI)
	{

		
		sendbuffer[31]=result->SignalStrength;
		sendbuffer[32]=result->OnLineFlag;	
	}


	
	crc=SumCalculate(sendbuffer,29);
	sendbuffer[33]=(uint8)(crc>>8);
	sendbuffer[34]=(uint8)(crc&0X00FF);

	#if 0
	printf("\n");
	for(i=0;i<22;i++)
	{
		printf("%02x:",sendbuffer[i]);
	}
	printf("\n");
	#endif

	SendDataToDisplayBoard(sendbuffer,sizeof(sendbuffer));
	
#if 0	
    while (ResppackRece == 0)
    {
        if ((i % 11 == 0) && (i > 0))
        {
            SendDataToDisplayBoard(sendbuffer,sizeof(sendbuffer));
        }
        HAL_Delay(30);
        ++i;
        if (i >= 11 * 3)
        {
            return ERR_FAIL;
        }
    }
    ResppackRece = 0;
    if (SumCheck(Commu_RX_BUF,13) == 0)
    {
        return ERR_FAIL;
    }

    if (Commu_RX_BUF[2] == datatype)
    {
        return ERR_SUCCESS;
    }

    return ERR_FAIL;
#endif	
}

void ProcessLcdUartData(void)
{

	if(gLCD_ReceData->len>0)
	{
		gLCD_ReceData->buf[gLCD_ReceData->len]=0;
		if (strncmp(gLCD_ReceData->buf,Cmd1Header,strlen(Cmd1Header)) == 0&&\
			strstr(gLCD_ReceData->buf, "OK")!=NULL )
		{
			gLCD_ReceData->processed=0;
			printf("len=%d\n",gLCD_ReceData->len);
			gLCD_ReceData->buf[gLCD_ReceData->len]=0 ;
			printf("%s\n",gLCD_ReceData->buf);	
			SendDataToDisplayBoard(Cmd1SuccessAck,sizeof(Cmd1SuccessAck));
			
			SysPara.thresholdVal.TemThreshold_a =UtilGetIntFlag(gLCD_ReceData->buf, "TThred_a:","\r\n");
			SysPara.thresholdVal.TemThreshold_b =UtilGetIntFlag(gLCD_ReceData->buf, "TThred_b:","\r\n");
			SysPara.thresholdVal.TemThreshold_c =UtilGetIntFlag(gLCD_ReceData->buf, "TThred_c:","\r\n");
			SysPara.thresholdVal.TemThreshold_s =UtilGetIntFlag(gLCD_ReceData->buf, "TThred_s:","\r\n");
			
			SysPara.thresholdVal.Vol_OverThreshold_a=UtilGetIntFlag(gLCD_ReceData->buf, "VOverThred_a:","\r\n");
			SysPara.thresholdVal.Vol_OverThreshold_b=UtilGetIntFlag(gLCD_ReceData->buf, "VOverThred_b:","\r\n");
			SysPara.thresholdVal.Vol_OverThreshold_c=UtilGetIntFlag(gLCD_ReceData->buf, "VOverThred_c:","\r\n");
			
			SysPara.thresholdVal.Vol_BelowThreshold_a=UtilGetIntFlag(gLCD_ReceData->buf, "VBelowThred_a:","\r\n");
			SysPara.thresholdVal.Vol_BelowThreshold_b=UtilGetIntFlag(gLCD_ReceData->buf, "VBelowThred_b:","\r\n");
			SysPara.thresholdVal.Vol_BelowThreshold_c=UtilGetIntFlag(gLCD_ReceData->buf, "VBelowThred_c:","\r\n");
			
			
			SysPara.thresholdVal.CurThreshold_a=UtilGetIntFlag(gLCD_ReceData->buf, "CThred_a:","\r\n");
			SysPara.thresholdVal.CurThreshold_b=UtilGetIntFlag(gLCD_ReceData->buf, "CThred_b:","\r\n");
			SysPara.thresholdVal.CurThreshold_c=UtilGetIntFlag(gLCD_ReceData->buf, "CThred_c:","\r\n");
			SysPara.thresholdVal.CurThreshold_r=UtilGetIntFlag(gLCD_ReceData->buf, "CThred_r:","\r\n");
			
			SysPara.thresholdVal.PowerThreshold=UtilGetIntFlag(gLCD_ReceData->buf, "PowerThred:","\r\n");

			memset(gLCD_ReceData->buf,Uart_ReceBufLen,0);
			gLCD_ReceData->len=0;
			gLCD_ReceData->processed=1;

			printf("--->>>����ֵ\nC=%d ",SysPara.thresholdVal.CurThreshold_a);
			printf("%d ",SysPara.thresholdVal.CurThreshold_b);	
			printf("%d ",SysPara.thresholdVal.CurThreshold_c);	
			printf("%d\n",SysPara.thresholdVal.CurThreshold_r); 		
			
			printf("V=%d ",SysPara.thresholdVal.Vol_OverThreshold_a);
			  printf("%d ",SysPara.thresholdVal.Vol_OverThreshold_b);	
			 printf("%d\n",SysPara.thresholdVal.Vol_OverThreshold_c);	
			
			  printf("%d ",SysPara.thresholdVal.Vol_BelowThreshold_a);
			  printf("%d ",SysPara.thresholdVal.Vol_BelowThreshold_b);
			 printf("%d\n",SysPara.thresholdVal.Vol_BelowThreshold_c);
			
			printf("T=%d ",SysPara.thresholdVal.TemThreshold_a);
			printf("%d ",SysPara.thresholdVal.TemThreshold_b);
			printf("%d ",SysPara.thresholdVal.TemThreshold_c);
			printf("%d\n",SysPara.thresholdVal.TemThreshold_s); 


			
			printf("save data\n");
			SysPara.thresholdVal_UsedDefault=Not_UsedDefaultValue;
			SysParaSave(InterFlashAddress,(uint8*)&SysPara,sizeof(Sys_Para));
		}
		else if (strncmp(gLCD_ReceData->buf,Cmd2SendDataToServer,strlen(Cmd2SendDataToServer)) == 0)
		{

			gLCD_ReceData->processed=0;
			printf("len=%d\n",gLCD_ReceData->len);
			gLCD_ReceData->buf[gLCD_ReceData->len]=0 ;
			printf("%s\n",gLCD_ReceData->buf);	
			SendDataToDisplayBoard(Cmd2SuccessAck,sizeof(Cmd2SuccessAck));

			printf("\nsend data  to senver\n");
			SysPara.sendDataToServerFlag=1;
			
			memset(gLCD_ReceData->buf,Uart_ReceBufLen,0);
			gLCD_ReceData->len=0;
			gLCD_ReceData->processed=1;


		}
		else if (strncmp(gLCD_ReceData->buf,Cmd3Header,strlen(Cmd3Header)) == 0)
		{
			gLCD_ReceData->processed=0;
			printf("len=%d\n",gLCD_ReceData->len);
			gLCD_ReceData->buf[gLCD_ReceData->len]=0 ;
			printf("%s\n",gLCD_ReceData->buf);
			SendDataToDisplayBoard(Cmd3SuccessAck,sizeof(Cmd3SuccessAck));
			if(UtilGetIntFlag(gLCD_ReceData->buf, "CurChannle_a:","\r\n")==1)
			{
				printf("Ca\n");
				SysPara.ChannleStatus.CurChannle_a=Channle_Status_Open;
			}
			else
			{
				SysPara.ChannleStatus.CurChannle_a=Channle_Status_Close;
			}


			
			if(UtilGetIntFlag(gLCD_ReceData->buf, "CurChannle_b:","\r\n")==1)
			{
				printf("Cb\n");
				SysPara.ChannleStatus.CurChannle_b=Channle_Status_Open;
			}
			else
			{
				SysPara.ChannleStatus.CurChannle_b=Channle_Status_Close;
			}

			
			if(UtilGetIntFlag(gLCD_ReceData->buf, "CurChannle_c:","\r\n")==1)
			{
				printf("Cc\n");
				SysPara.ChannleStatus.CurChannle_c=Channle_Status_Open;
			}
			else
			{
				SysPara.ChannleStatus.CurChannle_c=Channle_Status_Close;
			}

			
			if(UtilGetIntFlag(gLCD_ReceData->buf, "CurChannle_r:","\r\n")==1)
			{
				printf("Cr\n");
				SysPara.ChannleStatus.CurChannle_r=Channle_Status_Open;
			}
			else
			{
				SysPara.ChannleStatus.CurChannle_r=Channle_Status_Close;
			}	


			if(UtilGetIntFlag(gLCD_ReceData->buf, "VolChannle_a:","\r\n")==1)
			{
				printf("Va\n");
				SysPara.ChannleStatus.VolChannle_a=Channle_Status_Open;
			}
			else
			{
				SysPara.ChannleStatus.VolChannle_a=Channle_Status_Close;
			}	

			if(UtilGetIntFlag(gLCD_ReceData->buf, "VolChannle_b:","\r\n")==1)
			{
				printf("Vb\n");
				SysPara.ChannleStatus.VolChannle_b=Channle_Status_Open;
			}
			else
			{
				SysPara.ChannleStatus.VolChannle_b=Channle_Status_Close;
			}	

			if(UtilGetIntFlag(gLCD_ReceData->buf, "VolChannle_c:","\r\n")==1)
			{
				printf("Vc\n");
				SysPara.ChannleStatus.VolChannle_c=Channle_Status_Open;
			}
			else
			{
				SysPara.ChannleStatus.VolChannle_c=Channle_Status_Close;
			}				

			if(UtilGetIntFlag(gLCD_ReceData->buf, "TemChannle_a:","\r\n")==1)
			{
				printf("Ta\n");
				SysPara.ChannleStatus.TemChannle_a=Channle_Status_Open;
			}
			else
			{
				SysPara.ChannleStatus.TemChannle_a=Channle_Status_Close;
			}


			if(UtilGetIntFlag(gLCD_ReceData->buf, "TemChannle_b:","\r\n")==1)
			{
				printf("Tb\n");
				SysPara.ChannleStatus.TemChannle_b=Channle_Status_Open;
			}
			else
			{
				SysPara.ChannleStatus.TemChannle_b=Channle_Status_Close;
			}


			if(UtilGetIntFlag(gLCD_ReceData->buf, "TemChannle_c:","\r\n")==1)
			{
				printf("Tc\n");
				SysPara.ChannleStatus.TemChannle_c=Channle_Status_Open;
			}
			else
			{
				SysPara.ChannleStatus.TemChannle_c=Channle_Status_Close;
			}


			if(UtilGetIntFlag(gLCD_ReceData->buf, "TemChannle_s:","\r\n")==1)
			{
				printf("Ts\n");
				SysPara.ChannleStatus.TemChannle_s=Channle_Status_Open;
			}
			else
			{
				SysPara.ChannleStatus.TemChannle_s=Channle_Status_Close;
			}
			

			printf("--->>>ͨ������\nC=%02x ",SysPara.ChannleStatus.CurChannle_a);
			printf("%02x ",SysPara.ChannleStatus.CurChannle_b);
			printf("%02x ",SysPara.ChannleStatus.CurChannle_c);
			printf("%02x\n",SysPara.ChannleStatus.CurChannle_r);
			
			printf("V=%02x ",SysPara.ChannleStatus.VolChannle_a);
			printf("%02x ",SysPara.ChannleStatus.VolChannle_b);
			printf("%02x\n",SysPara.ChannleStatus.VolChannle_c);
			
			printf("T=%02x ",SysPara.ChannleStatus.TemChannle_a);
			printf("%02x ",SysPara.ChannleStatus.TemChannle_b);
			printf("%02x ",SysPara.ChannleStatus.TemChannle_c);
			printf("%02x\n",SysPara.ChannleStatus.TemChannle_s);

			printf("save data\n");
			SysPara.ChannleStatus_UsedDefault=Not_UsedDefaultValue;
			SysParaSave(InterFlashAddress,(uint8*)&SysPara,sizeof(Sys_Para));

			gLCD_ReceData->processed=1;
			gLCD_ReceData->len=0;


		}
		else if (strncmp(gLCD_ReceData->buf,Cmd4SuccessAck,strlen(Cmd4SuccessAck)) == 0)
		{
			printf("%s\n",Cmd4SuccessAck);
			gLCD_ReceData->len=0;
			SysPara.sendThresholdValueToLcd=0;
			SendAckToServer(0x03);
		}
		else if (strncmp(gLCD_ReceData->buf,Cmd5SuccessAck,strlen(Cmd5SuccessAck)) == 0)
		{
			printf("%s\n",Cmd5SuccessAck);
			gLCD_ReceData->len=0;
			SysPara.sendChannleEnableValueToLcd=0;
			SendAckToServer(0x05);
		}

	}



}
